package control
{
	import flash.events.Event;
	
	import model.ModelLocator;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.rpc.remoting.RemoteObject;
	public class savePlans
	{
		private var dbShow:showMyRoutes = new showMyRoutes();
		internal var theModel:ModelLocator = ModelLocator.getInstance();
		private var save:RemoteObject;
		private var saveAllPathHttp:HTTPService;
		private var saveRouteHttp:HTTPService;
		private var i:int;
		public function savePlans()
		{
			save = new RemoteObject();
			save.destination = "save";
			save.savePath.addEventListener(ResultEvent.RESULT,spResultHandler);
			save.saveRoute.addEventListener(ResultEvent.RESULT,srResultHandler);
			save.addEventListener(FaultEvent.FAULT, fHandler);
			save.modifyPath.addEventListener(ResultEvent.RESULT,ResultHandler);
			
		}
		public function savePath(userid:int,pathName:String,state:int,description:String):void{
			
			save.savePath(userid,pathName,state,description);
			
		}
		public function saveRoute(pathid:String,slat:Number,slng:Number,elat:Number,elng:Number,i:int):void{
			save.saveRoute(pathid,slat,slng,elat,elng,i);
		}
		public function saveAllPath(pathid:String,pathname:String,state:int,description:String):void{
			save.modifyPath(pathid,pathname,state,description);
		}
		private function httpEncoding(param:String):String{
                return encodeURIComponent(param);
        }
        internal function ResultHandler(event:ResultEvent):void{
        	
        	if(event.result as int>0){
        		
        		dbShow.show(theModel.user.user_id);
        		dbShow.showAll();
        		theModel.dispatchEvent(new Event(ModelLocator.SAVE_ALL_OK));
        		//dbShow.show(theModel.user.user_id);
        		
        	}
        }
		internal function spResultHandler(event:ResultEvent):void {
			
         	if(event.result!=0){
         			//Alert.show("here");
         			theModel.pathid=event.result.toString();
         			
         			dbShow.showAll();
					dbShow.show(theModel.user.user_id);
         			theModel.dispatchEvent(new Event(ModelLocator.SAVE_PATH_OK));
         	}
         	
         	else{
         		theModel.dispatchEvent(new Event(ModelLocator.SAVE_FAILED));
         	}
        }
       
        internal function srResultHandler(event:ResultEvent):void {
         	if(event.result>0 ){
         			theModel.dispatchEvent(new Event(ModelLocator.SAVE_ROUTE_OK));
         	}
         	
         	else{
         		
         			theModel.dispatchEvent(new Event(ModelLocator.SAVE_FAILED));
         		}
        }
		
		internal function fHandler (event:FaultEvent):void {
         		Alert.show(event.fault.message,"Error")
        }

	}
}